<?php

namespace App\Services\Home;


use App\Models\Article;
use App\Models\Handroom;
use App\Models\Home\Property;
use App\Models\HousePrice;
use App\Models\Region;
use App\Models\Rentalhouse;

class IndexService
{
    /**
     * @param $city_id
     * @param $date
     * 获取首页数据
     */
    public function getDataByCity($city_id)
    {
        // 热销信息
        $hot = Property::where('region',$city_id)->where('recommend', 'like', '%h%')->take(8)->get();
        // 获取同级城市
        $cityData = Region::findOrFail($city_id);
        $city = Region::where('parent_id', $cityData->parent_id)->get(['id','name']);
        //房价信息
        $data = $this->calAveragePrice($city_id);
        //文章信息
        $article = $this->getArticleData($city_id);

        $month = $data['month'];
        $average = $data['average'];
        $new_average = $data['new_average'];
        $hand_average = $data['hand_average'];
        $latestMonth = $data['latestMonth'];
        $rate = $data['rate'];

        return response()->json([
            'code' => 0,
            'hot' => $hot,
            'cityIndex' => $city,
            'month' => $month,
            'average' => $average,
            'newPrice' => $new_average,
            'handPrice' => $hand_average,
            'latestMonth' => $latestMonth,
            'rate' => $rate,
            'article' => $article
        ]);
    }

    /**
     * @param $city_id
     * 首页文章调用
     */
    public function getArticleData($city_id)
    {
        //项目动态 h -> 大图推荐
        $data['dt_rec'] = Article::latest()->where(function ($q) use($city_id) {
            $q->where('region_id',$city_id)
                ->orWhere('province',0);
        })->where('category_id',1)->where('recommend','like','%h%')->first();
        // 动态
        $data['dongtai'] = Article::latest()->where(function ($q) use($city_id) {
            $q->where('region_id',$city_id)
                ->orWhere('province',0);
        })->where('category_id',1)->take(8)->get();
        //好房推荐 h -> 大图推荐
        $data['hf_rec'] = Article::latest()->where(function ($q) use($city_id) {
            $q->where('region_id',$city_id)
                ->orWhere('province',0);
        })->where('category_id',2)->where('recommend','like','%h%')->first();
        // 好房
        $data['haofang'] = Article::latest()->where(function ($q) use($city_id) {
            $q->where('region_id',$city_id)
                ->orWhere('province',0);
        })->where('category_id',2)->take(8)->get();
        //楼市推荐 h -> 大图推荐
        $data['ls_rec'] = Article::latest()->where(function ($q) use($city_id) {
            $q->where('region_id',$city_id)
                ->orWhere('province',0);
        })->where('category_id',3)->where('recommend','like','%h%')->first();
        // 楼市
        $data['loushi'] = Article::latest()->where(function ($q) use($city_id) {
            $q->where('region_id',$city_id)
                ->orWhere('province',0);
        })->where('category_id',3)->take(8)->get();
        return $data;
    }

    public function calAveragePrice($city_id)
    {
        $new = HousePrice::where('region_id', $city_id)->where('type', 'newhouse')->orderBy('month', 'desc')->take(6)->get()->toArray();
        $hand = HousePrice::where('region_id', $city_id)->where('type', 'hand')->orderBy('month', 'desc')->take(6)->get()->toArray();

        //计算新房和二手房的均价
        $average = [];
        $month = [];
        // 循环数据多的那一项
        if(count($new) > 0 && count($new) >= count($hand)){
            foreach ($new as $k => $v) {
                if(!isset($hand[$k]['average'])){
                    $average[$k] = (int)$v['average'];
                }else{
                    $average[$k] = (int)(($v['average'] + $hand[$k]['average']) / 2);
                }
                $month[$k] = $v['month'].'月';
            }
        }elseif(count($hand) > 0 && count($hand) > count($hand)){
            foreach ($hand as $k => $v) {
                if(!isset($new[$k]['average'])){
                    $average[$k] = (int)$v['average'];
                }else{
                    $average[$k] = (int)(($v['average'] + $new[$k]['average']) / 2);
                }
                $month[$k] = $v['month'].'月';
            }
        }
        // 增长率
//        $last = $average[count($average)-2]??0;
//        $newer = $average[count($average)-1]??0;
        $last = $average[1]??0;
        $newer = $average[0]??0;
        if($newer>0 && $last>0){
            $data['rate'] = number_format((($newer-$last)/$last)*100,2);
        }else{
            $data['rate'] = 0;
        }

        if(count($hand)){
//            $data['hand_average'] = $hand[count($hand)-1]['average'];
            $data['hand_average'] = $hand[0]['average'];
        }else{
            $data['hand_average'] = 0;
        }
        if(count($new)){
//            $data['new_average'] = $new[count($new)-1]['average'];
            $data['new_average'] = $new[0]['average'];
        }else{
            $data['new_average'] = 0;
        }
        if(count($month)){
//            $data['latestMonth'] = $month[count($month)-1];
            $data['latestMonth'] = $month[0];
        }else{
            $data['latestMonth'] = (int)date('m');
        }
        // 反转数组
        $data['average'] = array_reverse($average);
        $data['month'] = array_reverse($month);
        return $data;
    }

    public function getAverageData($city_id)
    {
        $data = $this->calAveragePrice($city_id);
        $city = Region::findOrFail($city_id);
        return response()->json([
            'code' => 0,
            'month' => $data['month'],
            'average' => $data['average'],
            'newPrice' => $data['new_average'],
            'handPrice' => $data['hand_average'],
            'latestMonth' => $data['latestMonth'],
            'rate' => $data['rate'],
            'cityName' => $city->name
        ]);
    }

}